//
//  RTSearchView.swift
//  Weibo
//
//  Created by Brisinga on 16/3/2.
//  Copyright © 2016年 Root. All rights reserved.
//

import UIKit

class RTSearchView: UIView {

    @IBOutlet weak var cancelBtn: UIButton!
 
    @IBOutlet weak var searchField: UITextField!

    @IBOutlet weak var rightContraint: NSLayoutConstraint!
    
    
    // 构造方法
    class func searchView() -> RTSearchView {
        
        return NSBundle.mainBundle().loadNibNamed("RTSearchView", owner: nil, options: nil).last as! RTSearchView
    
    }
    
    
    
    
    // 初始化
    override func awakeFromNib() {
        
        leftView.frame = CGRect(x: 0, y: 0, width: searchField.frame.height, height: searchField.frame.height)
        
        searchField.leftView = leftView
        
        searchField.leftViewMode = .Always
    }
    
    
    

    @IBAction func cancelBtnDidClick(sender: UIButton) {
        
        searchField.resignFirstResponder()
        
        rightContraint.constant = 0
        
        UIView.animateWithDuration(0.25) { () -> Void in
            
            self.searchField.layoutIfNeeded()
        }
    }
    
    @IBAction func searchDidBegan(sender: AnyObject) {
        
        searchField.becomeFirstResponder()
        
        rightContraint.constant = cancelBtn.bounds.width
        
        UIView.animateWithDuration(0.25) { () -> Void in
            
            self.searchField.layoutIfNeeded()
        }
    }
    
    
    
    
    // 懒加载leftView
    private lazy var leftView: UIImageView = {
    
        let lfView = UIImageView(image: UIImage(named: "searchbar_textfield_search_icon"))
        
        lfView.contentMode = .Center
        
        return lfView
    
    }()
}
